//
//  SetVC.swift
//  UNHimalayas
//
//  Created by 王水平 on 2019/11/25.
//  Copyright © 2019 HachiTech. All rights reserved.
//

import UIKit

class SetVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tableView : UITableView!
    var dataArray = [[[String:String]()]]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.edgesForExtendedLayout = []
        self.navigationItem.title = "设置"
        self.view.backgroundColor = RGBA(240, g: 240, b: 240, a: 1.0);
        
        initAction()
        initTabelView()
        
        dataArray = [
            [
                ["title":"智能硬件","content":"","switch":"0"]
            ],
            [
                ["title":"物色闹铃","content":"","switch":"0"]
            ],
            [
                ["title":"账号和安全","content":"","switch":"0"]
            ],
            [
                ["title":"推送设置","content":"","switch":"0"],
                ["title":"隐私","content":"","switch":"0"],
                ["title":"收听偏好设置","content":"","switch":"0"],
                ["title":"Siri捷径管理","content":"","switch":"0"],
            ],
            [
                ["title":"断点续听","content":"","switch":"1"],
                ["title":"2G/3G/4G播放和下载","content":"","switch":"1"],
                ["title":"列表结束智能推荐播放","content":"","switch":"1"],
                ["title":"下载音质","content":"","switch":"0"],
                ["title":"清理占用空间","content":"100.1M","switch":"0"],
            ],
            [
                ["title":"特色功能","content":"","switch":"0"],
                ["title":"新版本介绍","content":"","switch":"0"],
                ["title":"给吉马拉雅好评","content":"","switch":"0"],
                ["title":"关于","content":"","switch":"0"],
            ],
          ]
        tableView.reloadData()
    }
    
    func initAction() -> Void {
        
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "navi_back"), for: UIControl.State.normal)
        leftBtn.setImage(UIImage(named: "navi_back"), for: UIControl.State.highlighted)
           
        leftBtn.addTarget(self, action: #selector(SetVC.leftAction), for: UIControl.Event.touchUpInside)
        // button自适应大小
        leftBtn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView:leftBtn)
    }
    
    func initTabelView() -> Void {
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0), style: .grouped)
        self.tableView.backgroundColor = UIColor.clear
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.view.addSubview(self.tableView)
        
        self.tableView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self.view)
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellName = "SetViewCellName"
        let cell = SetViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellName)
        
        let dict = dataArray[indexPath.section][indexPath.row]
        
        cell.titleLabel.text = dict["title"]
        cell.contentLabel.text = dict["content"]

        let str = dict["switch"]
        cell.setSwitch.isHidden = str == "0"
        cell.arrowsImgView.isHidden =  str == "1"
        
        return cell;
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
     
        return nil;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kRealWidth(50)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return kRealWidth(10)
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        pushVC(indexPath: indexPath)
    }

    // MARK: - 点击
    
    @objc private func leftAction() -> Void {
        
        self.navigationController?.popViewController(animated: true)
    }
    
    // MARK: - 界面跳转
    
    func pushVC(indexPath : IndexPath) -> Void {
        
        if indexPath.section == 0 {
            
            //智能硬件
            
        }
        else if indexPath.section == 1 {
            
            //特色闹铃
            let vc = AlarmClockVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        else if indexPath.section == 2 {
            
            //账号与安全
            let vc = AccountAndSecurityVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        else if indexPath.section == 3 {
            
            if indexPath.row == 0 {
                
                //推送设置
                let vc = PushSettingsVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            else if indexPath.row == 1 {
                
                //隐私
                let vc = PrivacySettingsVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            else if indexPath.row == 2 {
                
            }
            else if indexPath.row == 3 {
                
                //Siri捷径管理
                let vc = SiriManageVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
        else if indexPath.section == 4 {
            
            if indexPath.row == 0 {
                 
            }
            else if indexPath.row == 1 {
                
            }
            else if indexPath.row == 2 {
                
            }
            else if indexPath.row == 3 {
                
                //下载音质
                let vc = DownloadSoundQualitVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            else if indexPath.row == 4 {
                
            }
        }
        else if indexPath.section == 5 {
            
            if indexPath.row == 0 {
                
                
            }
            else if indexPath.row == 1 {
                
            }
            else if indexPath.row == 2 {
                
            }
            else if indexPath.row == 3 {
                
                //关于
                let vc = SetAboutVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }

}
